<?php
require_once(dirname(__FILE__)."/../common/constants.php");
require_once(TRANSACTIONS_DIR_PATH."/calendarmanager.php");
// Set the content-type header

function c_unescape($str)
{ 
	$str = rawurldecode($str); 
	preg_match_all("/%u.{4}|&#x.{4};|&#d+;|.+/U",$str,$r);
	$ar = $r[0]; 
	foreach($ar as $k=>$v)
	{ 
		if(substr($v,0,2) == "%u")        $ar[$k] = iconv("UCS-2","GBK",pack("H4",substr($v,-4))); 
		elseif(substr($v,0,3) == "&#x")    $ar[$k] = iconv("UCS-2","GBK",pack("H4",substr($v,3,-1))); 
		elseif(substr($v,0,2) == "&#")    $ar[$k] = iconv("UCS-2","GBK",pack("n",substr($v,2,-1)));
	}
	return join("",$ar); 
}

if (isset($_GET["uid"])) {
	CalendarManager::deleteByUid($_GET["uid"]);
}
    
/*
header("Content-Type:text/xml; charset=utf-8");

$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$list = $doc->createElement("list");
$list->appendChild($doc->createTextNode("²㋔"));
$doc->appendChild($list);
*/

// Output
//echo $doc->saveXML();

//$out = "CODE\t200\n";
//$out .= "MESSAGE\t2008-07-3 02:45:00\tǏ͑Ҁ - µ¹�
//echo $out;

/* Query the calendars by this week. */

// Get the day of the week
/*
$today = getdate();
$year = $today["year"];
$mon = $today["mon"];
$dayOfWeek = $today['wday'];
$dateOfToday = $today['mday'];

$delta2Friday = 5 - $dayOfWeek;
$dateOfFriday = $dateOfToday + $delta2Friday;

$lastDay = 31;
if ($mon == 4 ||
	$mon == 6 ||
	$mon == 9 ||
	$mon == 11) {
	$lastDay = 30;
} else if ($mon == 2) {
	if ($year % 4 == 0 && ( $year % 100 != 0 || $year % 400 == 0)) {
		$lastDay = 29;
	} else {
		$lastDay = 28;
	}
}
if ($dateOfFriday > $lastDay) {
	$dateOfFriday = $dateOfFriday - $lastDay;
	$mon++;
}

$timeOfFriday = strtotime($year.'-'.$mon.'-'.$dateOfFriday);
$range = 4 * 24 * 60 * 60;
$endTime = $timeOfFriday + $range;
*/
$endTime = time() + 30 * 24 * 60 * 60;

$cs = CalendarManager::queryByTime('0', $endTime.'000');

echo '<table>';
foreach ($cs as $item) {
	echo '<tr>';
	$ts = $item->ts_start;	
	$len = strlen($ts) - 3;
	$ts = substr($ts, 0, $len);
	$datetime = date("Y-m-d H:i:s", $ts);
	echo '<td width=200>'.$datetime.'</td>';
	$sum = c_unescape($item->summary);
	echo '<td width=200>'.$sum.'</td>';
	echo '<td><a href="ls.php?uid='.$item->uid.'">delete</a></td>';
	echo '</tr>';
}
echo '</table>';
?>